package com.mvp.mvpdemo.mvp.model

import com.mvp.mvpdemo.Const
import com.mvp.mvpdemo.app.App
import com.mvp.mvpdemo.base.BaseModel
import com.mvp.mvpdemo.bean.LoginBean
import com.mvp.mvpdemo.bean.UpLoadFile
import com.mvp.mvpdemo.mvp.contract.LoginContract
import com.mvp.mvpdemo.net.APIService
import com.retrofit.retrofitutil.OkHttpLogInterceptor
import com.retrofit.retrofitutil.RetrofitBuilder
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import retrofit2.Call
import retrofit2.converter.gson.GsonConverterFactory
import java.io.File
import java.util.*


/**
 * author    : mahongyin
 * e-mail    : mhy.work@foxmail.com
 * date      : 2020-10-16 16:15
 * introduce :
 */
class LoginModel : BaseModel(), LoginContract.Model {

    override fun login(phone: String, pwd: String): Call<LoginBean> {
        return apiService.login2(phone, pwd)
    }

//Retrofit原生支持kotlin协程
    override suspend fun loginCC(phone: String, pwd: String): LoginBean {
        val retrofit = RetrofitBuilder.instance
            .addInterceptor(OkHttpLogInterceptor())
            .baseUrl(Const.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())//json解析的实体
            .build(App.getInstance().applicationContext)
        val apiService= retrofit.create(APIService::class.java)
        return apiService.loginCC(phone, pwd)
    }

    override fun logout(): Call<LoginBean> {
        return apiService.logout()
    }




    override fun upLoadFile(file: File): Call<UpLoadFile>? {
        val requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        val body: RequestBody = MultipartBody.Builder()
            .addFormDataPart("user", "myname")
            .addFormDataPart("token", "mynameabc")
            .addFormDataPart("file", file.name, requestBody)
            .build()
        return apiService.upload3(body)
    }

    override fun upLoadFiles(user: String, token: String, file: File): Call<UpLoadFile> {

        val map = TreeMap<String, RequestBody>()
        val userBody = RequestBody.create(MediaType.parse("text/plain;charset=UTF-8"), user)
        val tokenBody = RequestBody.create(MediaType.parse("text/plain;charset=UTF-8"), token)
        map.put("user", userBody)
        map.put("token", tokenBody)
        val requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);

//       return apiService.upload(map, requestBody)//1

//        map.put("file[]", requestBody)//2
//        return apiService.upload2(map)//2

//        val fileMap=TreeMap<String, File>()//3
//        fileMap["file"]=file//3
//       return upload(user,token,fileMap)//3

        val part = MultipartBody.Part.createFormData("file", file.name, requestBody)//4
        return apiService.upload4(userBody, tokenBody,part)//4
    }

    fun upload(user: String, token: String, fileMap: TreeMap<String, File>): Call<UpLoadFile> {
        val fileList = createMultipartBodyPartList(fileMap)
        val paparm = TreeMap<String, RequestBody>()
        val userBody = RequestBody.create(MediaType.parse("text/plain;charset=UTF-8"), user)
        val tokenBody = RequestBody.create(MediaType.parse("text/plain;charset=UTF-8"), token)
        paparm["user"] = userBody
        paparm["token"] = tokenBody
        return apiService.upload(paparm, fileList)

    }

    fun createMultipartBodyPartList(fileMap: Map<String, File>): List<MultipartBody.Part> {
        val partList = arrayListOf<MultipartBody.Part>()
        for (key in fileMap.keys) {
            val file = fileMap[key]
            file?.apply {
                val requestBody = RequestBody.create(MediaType.parse("image/*"), this)
                val part = MultipartBody.Part.createFormData(key, this.name, requestBody)
                partList.add(part)
            }
        }
        return partList
    }




}